<template>
    <div class="app-container">
        <el-card class="operate-container" shadow="never" v-if="type !== 'stand'">
            <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
                <el-form-item label="搜索：">
                    <el-input v-model="listQuery.searchKey" class="input-width" placeholder="请输入用户昵称/真实姓名" clearable></el-input>
                </el-form-item>
            </el-form>
            <div style="display: flex;">
                <el-button
                    style="float:right"
                    type="primary"
                    @click="handleSearchList()"
                    size="small">
                    查询搜索
                </el-button>
                <el-button
                    style="float:right;margin-right: 15px"
                    @click="handleResetSearch()"
                    size="small">
                    重置
                </el-button>
            </div>
        </el-card>
        <div class="table-container">
            <el-table ref="adminTable"
                    :data="list"
                    style="width: 100%;"
                    v-loading="listLoading" border>
                <el-table-column label="用户ID" width="180" align="center">
                    <template slot-scope="scope">{{scope.row.userId}}</template>
                </el-table-column>
                <el-table-column label="用户昵称" align="center">
                    <template slot-scope="scope">
                        <!-- {{scope.row.nikeName}} -->
                        <div v-html="hightLight(listQuery.searchKey, scope.row.nikeName)"></div>
                    </template>
                </el-table-column>
                <el-table-column label="真实姓名" align="center">
                    <template slot-scope="scope">
                        <!-- {{scope.row.realName }} -->
                        <div v-html="hightLight(listQuery.searchKey, scope.row.realName)"></div>
                    </template>
                </el-table-column>
                <el-table-column label="电话号码" width="160" align="center">
                    <template slot-scope="scope">（{{ scope.row.areaCode }}）{{scope.row.telephone}}</template>
                </el-table-column>
                <el-table-column label="电子邮箱" width="160" align="center">
                    <template slot-scope="scope">{{scope.row.email}}</template>
                </el-table-column>
                <el-table-column label="邀请ID" width="100" align="center">
                    <template slot-scope="scope">{{scope.row.inviteCode}}</template>
                </el-table-column>
                <el-table-column  v-if="type === 'dismiss'" label="通过认证时间" width="180" align="center">
                    <template slot-scope="scope">{{scope.row.approvalTime}}</template>
                </el-table-column>
                <el-table-column  v-if="type === 'dismiss'" label="审批人" width="100" align="center">
                    <template slot-scope="scope">{{scope.row.approvalUserName}}</template>
                </el-table-column>
                <el-table-column  v-if="type === 'dismiss'" label="撤销时间" width="180" align="center">
                    <template slot-scope="scope">{{scope.row.revokeTime}}</template>
                </el-table-column>
                <el-table-column  v-if="type === 'refuse'" label="申请时间" width="180" align="center">
                    <template slot-scope="scope">{{scope.row.createTime}}</template>
                </el-table-column>
                <el-table-column  v-if="type === 'refuse'" label="审批人" width="100" align="center">
                    <template slot-scope="scope">{{scope.row.approvalUserName}}</template>
                </el-table-column>
                <el-table-column  v-if="type === 'refuse'" label="拒绝时间" width="180" align="center">
                    <template slot-scope="scope">{{scope.row.approvalTime}}</template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center" v-if="type === 'stand'">
                    <template slot-scope="scope">
                        <el-button size="mini"
                                    type="text"
                                    @click="handleUpdate(scope.$index, scope.row)">详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="pagination-container">
            <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                layout="total, sizes,prev, pager, next,jumper"
                :current-page.sync="listQuery.pageNum"
                :page-size="listQuery.pageSize"
                :page-sizes="[10,15,20]"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog
            :title="'票务顾问详情'"
            :visible.sync="visible"
            width="55%">
            <el-form :model="detailInfo"
                    ref="thirdForm"
                    :rules="rules"
                    label-width="200px" size="small">
                <el-form-item label="用户昵称：">
                    <span>{{ detailInfo.nikeName }}</span>
                </el-form-item>
                <el-form-item label="真实姓名：">
                    <span>{{ detailInfo.realName }}</span>
                </el-form-item>
                <el-form-item label="电话号码：">
                    <span>（{{ detailInfo.areaCode }}）{{ detailInfo.telephone }}</span>
                </el-form-item>
                <el-form-item label="电子邮箱：">
                    <span>{{ detailInfo.email }}</span>
                </el-form-item>
                <el-form-item label="邀请ID：">
                    <span>{{ detailInfo.inviteCode }}</span>
                </el-form-item>
                <el-form-item label="注册时间：">
                    <span>{{ detailInfo.registerTime }}</span>
                </el-form-item>
                <el-form-item label="注册IP：">
                    <span>{{ detailInfo.registerIpAddress }}（{{detailInfo.registerIpRegion}}）</span>
                </el-form-item>
                <el-form-item label="申请时间：">
                    <span>{{ detailInfo.createTime }}</span>
                </el-form-item>
                <el-form-item label="票务顾问类型：" prop="userTicketType">
                    <el-radio-group v-model="detailInfo.userTicketType">
                        <el-radio label="REAL">真实票务</el-radio>
                        <el-radio label="COMPANY">公司员工</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <!-- <el-button @click="visible = false" size="small">认证通过</el-button> -->
                <el-button type="primary" @click="dialogConfirm('thirdForm', 'PASS')" size="small">认证通过</el-button>
                <el-button type="primary" @click="dialogConfirm('thirdForm', 'REJECT')" size="small">拒绝认证</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { approvalList, approvalInfo, approvalHandler, fetchAllResourceList } from '@/api/app';
export default {
    data() {
        return {
            listLoading: false,
            list: [],
            listQuery: {
                pageNum: 1,
                pageSize: 10,
                approveStatus: 'DOING',
                searchKey: null,
            },
            total: 0,
            type: 'stand',
            detailInfo: {
                approvalTime: null,
                approvalUserName: null,
                approveStatus: null,
                areaCode: null,
                createTime: null,
                currency: null,
                email: null,
                id: null,
                inviteCode: null,
                nikeName: null,
                realName: null,
                registerIpAddress: null,
                registerIpRegion: null,
                registerTime: null,
                revokeTime: null,
                revokeUserName: null,
                telephone: null,
                userId: null,
                userTicketType: null,
            },
            visible: false,
            rules: {
                userTicketType: [
                    { required: true, message: '请选择审核处理类型' }
                ],
            },
            btnMenu: {
                edit: true,
                delete: true,
            },
        }
    },
    watch: {
        $route(to, from) {
            const menu = JSON.parse(localStorage.getItem('listMenu'));
            this.type = to.name;
            if (this.type === 'dismiss') {
                this.listQuery.approveStatus = 'REVOKE';
                const item = menu.filter(item => item.title === '已撤销')[0];
                this.menuList('dismiss', item);
            } else if (this.type === 'refuse') {
                this.listQuery.approveStatus = 'REJECT';
                const item = menu.filter(item => item.title === '已拒绝')[0];
                this.menuList('refuse', item);
            } else {
                this.listQuery.approveStatus = 'DOING';
                const item = menu.filter(item => item.title === '待审核')[0];
                this.menuList('stand', item);
            }
            this.getList();
        }
    },
    mounted() {

    },
    created() {
        const menu = JSON.parse(localStorage.getItem('listMenu'));
        const href = location.href;
        if(href.includes('stand')) {
            this.type ='stand';
            this.listQuery.approveStatus = 'DOING';

            const item = menu.filter(item => item.title === '待审核')[0];
            this.menuList('stand', item);
        }else if (href.includes('dismiss')) {
            this.type ='dismiss';
            this.listQuery.approveStatus = 'REVOKE';

            const item = menu.filter(item => item.title === '已撤销')[0];
            this.menuList('dismiss', item);
        }else if (href.includes('refuse')) {
            this.type = 'refuse';
            this.listQuery.approveStatus = 'REJECT';

            const item = menu.filter(item => item.title === '已拒绝')[0];
            this.menuList('refuse', item);
        };
        this.getList();
    },
    methods: {
        menuList(type, item) {
            fetchAllResourceList({menuId: item.id }).then(res => {
                res.data.forEach(dom => {
                if (dom.component === '' && dom.name === '详情') {
                    this.btnMenu.edit = false;
                }
                if (dom.component === '' && dom.name === '审核') {
                    this.btnMenu.delete = false;
                }
                });
            });
        },
        hightLight (keyWord, suggtion) {
            // 使用 regexp 构造函数，因为这里要匹配的是一个变量
            let newSrt = null;
            const reg = new RegExp(keyWord, 'ig')
            if (keyWord && suggtion) {
                newSrt = String(suggtion).replace(reg, function (p) {
                    return '<span style="color: #409EFF">' + p + '</span>'
                })
            } else {
                newSrt = suggtion;
            }
            return newSrt
        },
        getList() {
            approvalList(this.listQuery).then(res => {
                this.list = res.data.records;
                this.total = res.data.total;
            });
        },
        handleResetSearch() {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = 10;
            this.listQuery.searchKey = null;
          this.getList();
        },
        handleSearchList() {
            this.listQuery.pageNum = 1;
            this.getList();
        },
        handleUpdate(index, row) {
            if (this.btnMenu.edit) {
                approvalInfo(row.id).then(res => {
                    this.detailInfo = res.data;
                    this.visible = true;
                });
            } else {
                this.$message({
                    type: 'error',
                    message: '无权限操作'
                });
            }
        },
        handleSizeChange(val) {
          this.listQuery.pageNum = 1;
          this.listQuery.pageSize = val;
          this.getList();
        },
        handleCurrentChange(val) {
          this.listQuery.pageNum = val;
          this.getList();
        },
        dialogConfirm(formName, type) {
            if (this.btnMenu.delete) {
                this.$refs[formName].validate((valid) => {
                    if (type === 'PASS') {
                        if (valid) {
                            approvalHandler({id: this.detailInfo.id, userTicketType: this.detailInfo.userTicketType, approveStatus: type}).then(res => {
                                this.getList();
                                this.visible = false;
                            })
                        }
                    } else {
                        approvalHandler({id: this.detailInfo.id, userTicketType: this.detailInfo.userTicketType, approveStatus: type}).then(res => {
                            this.getList();
                            this.visible = false;
                        })
                    }
                })
            } else {
                this.$message({
                    type: 'error',
                    message: '无权限操作'
                });
            }
        }
    },
}
</script>

<style lang="scss">
</style>
